package com.yoyo.itravel.service.impl;

import com.yoyo.itravel.bean.Flight;
import com.yoyo.itravel.dao.IFlightDao;
import com.yoyo.itravel.exceptions.FlightException;
import com.yoyo.itravel.service.IFlightService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.SQLException;
import java.util.List;

/**
 * @author liuchaojie
 * @version 1.0
 * @Description 航班表服务层接口实现类【服务层】
 * @date 2020/6/18 17:19
 */
@Service
public class FlightServiceImpl implements IFlightService {
    @Autowired
    private IFlightDao flightDao;

    /**
     * 新增一个航班
     * @param flight
     * @return
     * @throws FlightException
     */
    @Override
    public boolean insertFlight(Flight flight) throws FlightException {
        try {
            return flightDao.insertFlight(flight);
        } catch (SQLException e) {
            e.printStackTrace();
            throw new FlightException("插入航班信息表失败！...");
        }
    }

    /**
     * 根据航班id删除航班
     * @param flightId
     * @return
     * @throws FlightException
     */
    @Override
    public boolean deleteByFlightId(String flightId) throws FlightException {
        try {
            return flightDao.deleteByFlightId(flightId);
        } catch (SQLException e) {
            e.printStackTrace();
            throw new FlightException("根据航班id删除航班信息表失败！...");
        }
    }

    /**
     * 查找所有航班信息失败
     * @return
     * @throws FlightException
     */
    @Override
    public List<Flight> selectAll() throws FlightException {
        try {
            return flightDao.selectAll();
        } catch (SQLException e) {
            e.printStackTrace();
            throw new FlightException("查找所有航班信息失败！...");
        }
    }

    /**
     * 根据 airlineId、departCity、arrivalCity、departTime查询航班
     * @param flight
     * @return
     * @throws FlightException
     */
    @Override
    public List<Flight> selectByCondition(Flight flight) throws FlightException {
        try {
            return flightDao.selectSomeByCondition(flight);
        } catch (SQLException e) {
            e.printStackTrace();
            throw new FlightException("查询航班失败！...");
        }
    }

    /**
     * 更新航班
     * @param flight
     * @return
     * @throws FlightException
     */
    @Override
    public boolean updateFlight(Flight flight) throws FlightException {
        try {
            return 1 == flightDao.updateFlight(flight);
        } catch (SQLException e) {
            e.printStackTrace();
            throw new FlightException("更新航班失败！...");
        }
    }

    /**
     * 更新航班的在售状态
     * @param flightId
     * @param isOnSell
     * @return
     * @throws FlightException
     */
    @Override
    public boolean updateIsOnSell(String flightId, Integer isOnSell) throws FlightException {
        try {
            return 1 == flightDao.updateIsOnSell(flightId,isOnSell);
        } catch (SQLException e) {
            e.printStackTrace();
            throw new FlightException("更新在售状态失败！...");
        }
    }
}
